package com.zzh.tree;



public class No116_connect {
    public static void main(String[] args) {

    }
}
class No116Solution{
    //此方法通过已经建立好的next为下一层接着建立next
    public Node connect(Node root) {
        if (root==null){
            return null;
        }
        Node head=root;
        while (head!=null){
            Node level=new Node();
            Node tail=level;
            while (head!=null){
                if (head.left!=null){
                    tail.next=head.left;
                    tail=tail.next;
                }
                if (head.right!=null){
                    tail.next=head.right;
                    tail=tail.next;
                }
                head=head.next;
            }
            head=level.next;
        }
        return root;
    }
}
